package us.pigsoft.client;

import java.awt.Color;
import java.awt.Dimension;

public class Game extends Thread {
	
	public enum State {
		MainMenu,
		PauseMenu,
		Game,
		GameOver,
		Settings,
		Length
	};
	
	MainInterface app;
	boolean running;
	State currentState;
	GuiInterface[] interfaces;
	double fps = 60.0;

	public Game(MainInterface applet)
	{
		app = applet;
		currentState = State.MainMenu;
		interfaces = new GuiInterface[State.Length.ordinal()];
		
		GuiInterface mainInterface = new GuiInterface(app);
		mainInterface.addElement(new GuiPanel(10, 10, new Dimension(app.size.width-20, 480-20), Color.GREEN, "mainPanel"));
		mainInterface.addElement(new GuiButton(220, 250, new Dimension(200, 40), Color.cyan, Color.cyan, "play", "playButton"));
		mainInterface.addElement(new GuiLabel(70, 40, new Dimension(500, 70), Color.cyan, "GEOSHOCK"));
		interfaces[State.MainMenu.ordinal()] = mainInterface;
		
		GuiInterface gameInterface = new GuiInterface(app);
		gameInterface.addElement(new GuiPanel(10, 10, new Dimension(app.size.width-20,480-20), Color.cyan, "mainPanel"));
		gameInterface.addElement(new GuiPanel(10, 480 , new Dimension(app.size.width-20, 100 - 10), Color.cyan, "mainPanel"));
		gameInterface.addElement(new GuiGame(11, 11, new Dimension(app.size.width-21, 480-21), null, "gameArea"));
		interfaces[State.Game.ordinal()] = gameInterface;
		
		running = true;
	}
	
	@Override
	public void run()
	{
		while(true)
		{
			double timeStep = System.currentTimeMillis();
			
			//set current gui pointer to the gui of the current states
			if(app.gui != interfaces[currentState.ordinal()])
				app.gui = interfaces[currentState.ordinal()];
			
			if(currentState == State.MainMenu)
			{
				app.repaint();
			}
			else if(currentState == State.Game)
			{
				//app.gui = interfaces[State.Game.ordinal()];
				app.gui.update();
				app.repaint();
			}
			
			
			//wait for next frame
			try {
				while(System.currentTimeMillis()-timeStep < 1000/fps) {
					sleep(1);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
